package com.harboursoftware.xstorage.unit.results;

import java.util.Calendar;

import com.harboursoftware.xstorage.model.XBucket 
import com.harboursoftware.xstorage.model.XObject 
import com.harboursoftware.xstorage.model.XUser 
import com.harboursoftware.xstorage.results.GetBucketResult 
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test 

/**
 * test case from 
 * <a href="http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html">GET Bucket (List Objects)</a>
 */
public class GetBucketResultTest {
	def getBucketResult
	
	@Before
	void setUp() {
		def calendar1  = Calendar.getInstance()
		calendar1.clear()
        //notes : this tiem is 2009-10-12
		calendar1.set(2009, 9, 12, 17, 50, 30)
        def owner1 = new XUser(id : '8a6925ce4a7f21c32aa379004fef', displayName : 'mtd@amazon.com')
		def object1 = new XObject(key : 'my-image.jpg', lastModified : calendar1.timeInMillis, etag : 'fba9dede5f27731c9771645a39863328', length : 434234, owner : owner1)
		
		def calendar2  = Calendar.getInstance()
		calendar2.clear()
        //notes : this tiem is 2009-10-12
		calendar2.set(2009, 9, 12, 17, 50, 30)
        def owner2 = new XUser(id : '8a69b1ddee97f21c32aa379004fef', displayName : 'mtd@amazon.com')
		def object2 = new XObject(key : 'my-third-image.jpg', lastModified : calendar1.timeInMillis, etag : '1b2cf535f27731c974343645a3985328', length : 64994, owner : owner2)
		
        def bucket = new XBucket(name : 'bucket')
        getBucketResult = new GetBucketResult()
        getBucketResult.bucket = bucket
        getBucketResult.maxKeys = 1000
		getBucketResult.bucket.objects = [object1, object2]
        getBucketResult.bucket.size = 2
	}
	
	@After
	void tearDown() {
		getBucketResult = null
	}
	
	@Test
	void test_toXML() {
        def expected = '<?xml version="1.0" encoding="UTF-8"?><ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01"><Name>bucket</Name><Prefix/><Marker/><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>my-image.jpg</Key><LastModified>2009-10-12T17:50:30.000Z</LastModified><ETag>&quot;fba9dede5f27731c9771645a39863328&quot;</ETag><Size>434234</Size><StorageClass>STANDARD</StorageClass><Owner><ID>8a6925ce4a7f21c32aa379004fef</ID><DisplayName>mtd@amazon.com</DisplayName></Owner></Contents><Contents><Key>my-third-image.jpg</Key><LastModified>2009-10-12T17:50:30.000Z</LastModified><ETag>&quot;1b2cf535f27731c974343645a3985328&quot;</ETag><Size>64994</Size><StorageClass>STANDARD</StorageClass><Owner><ID>8a69b1ddee97f21c32aa379004fef</ID><DisplayName>mtd@amazon.com</DisplayName></Owner></Contents></ListBucketResult>'
        Assert.assertEquals expected, getBucketResult.toXML()
    }

}
